package com.whw.springboot.walk.service.servicelmpl;

import com.whw.springboot.walk.entity.Action;
import com.whw.springboot.walk.entity.Apply;
import com.whw.springboot.walk.entity.Message;
import com.whw.springboot.walk.mapper.ActionMapper;
import com.whw.springboot.walk.mapper.ApplyMapper;
import com.whw.springboot.walk.service.ApplyService;
import com.whw.springboot.walk.utils.ApplyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ApplyServiceImpl implements ApplyService {

    @Resource
    private ApplyMapper applyMapper;
    @Resource
    private ActionMapper actionMapper;

    @Override
    public Message<?> addApply(Apply apply) {
        System.out.println(apply.toString());
        String applyId = "AP"+ApplyUtil.genApplyId();
        apply.setApplyId(applyId);
        apply.setApplyCreateTime(new Date());
        Action action = actionMapper.findActionByPrimaryKey(apply.getApplyActId());
        Action action2 = new Action();
        action2.setActId(action.getActId());
        action2.setActJoinNum(action.getActJoinNum() +1);
        actionMapper.updateActionByPrimaryKey(action2);
        return applyMapper.addApply(apply)>0? Message.success(null).add(applyId):Message.fail(null);
    }

    @Override
    public Message<?> deleteApplyByPrimaryKey(String applyId) {
        Apply apply = applyMapper.selectAllApplyByPrimaryKey(applyId);
        Action action = actionMapper.findActionByPrimaryKey(apply.getApplyActId());
        int num = action.getActJoinNum() -1;
        action.setActJoinNum(num);
        actionMapper.updateActionByPrimaryKey(action);
        return applyMapper.deleteApplyByPrimaryKey(applyId)>0? Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> deleteApplyByActId(String actId) {
        return applyMapper.deleteApplyByActId(actId)>0? Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> deleteApplyByUserId(String userId) {
        return applyMapper.deleteApplyByUserId(userId)>0? Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> deleteApplyByActIdAndUserId(Apply apply) {
        Action action = actionMapper.findActionByPrimaryKey(apply.getApplyActId());
        Action action2 = new Action();
        action2.setActId(action.getActId());
        action2.setActJoinNum(action.getActJoinNum() -1);
        actionMapper.updateActionByPrimaryKey(action2);
        return applyMapper.deleteApplyByActIdAndUserId(apply)>0? Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> updateApplyPrimaryKey(Apply apply) {
        return applyMapper.updateApplyPrimaryKey(apply)>0? Message.success(null):Message.fail(null);
    }

    @Override
    public Message<?> updateApplyByPrimaryKeySelective(Apply apply) {
        return applyMapper.updateApplyByPrimaryKeySelective(apply)>0? Message.success(null):Message.fail(null);
    }

    @Override
    public Message<List<Apply>> selectAllApply() {
        return Message.success(null).add(applyMapper.selectAllApply());
    }

    @Override
    public Message<List<Apply>> selectAllApplyByUserId(String userId) {
        return Message.success(null).add(applyMapper.selectAllApplyByUserId(userId));
    }

    @Override
    public Message<List<Apply>>  selectAllApplyByActId(String actId) {
        return Message.success(null).add(applyMapper.selectAllApplyByActId(actId));
    }

    @Override
    public Message<Apply> selectApplyByUserIdAndActId(String userId, String actId) {
        return Message.success(null).add(applyMapper.selectApplyByUserIdAndActId(userId,actId));
    }

    @Override
    public Message<Apply> selectApplyByPrimaryKey(String applyId) {
        return Message.success(null).add(applyMapper.selectAllApplyByPrimaryKey(applyId));
    }
}
